#include<bits/stdc++.h>
using namespace std;
int n,f[400005],s[400005],s2[400005],g[400005],ans;
int find(int x){
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
signed main(){
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=2*n;i++) f[i]=i,s[i]=1;
	for(int i=1;i<=n;i++){
		int u,v;
		cin>>u>>v;
		g[u]=g[v]=1;
		u=find(u); v=find(v);
		if(u==v) {s2[u]++;continue;}
		s[v]+=s[u];
		s2[v]+=s2[u]+1;
		f[u]=v;
	}
	for(int i=1;i<=2*n;i++){
		if(find(i)==i && g[i]==1){
			// cout<<" > "<<i<<" : "<<s[i]<<" "<<s2[i]<<'\n';
			ans+=min(s[i],s2[i]);
		}
	}
	cout<<ans;
	return 0;
}